<?php


namespace App\Http\Controllers;


use App\Models\Video;
use App\Models\VoteLog;
use Illuminate\Http\Request;

class VideoController extends Controller
{
    public function list(Request $request)
    {
        $videos = Video::all();

        if (empty($videos)) {
            return $this->responseJson(200);
        }

        $data = [];

        $userID = $request->cookie('login-token');

        $videoIDs = [];

        if (!empty($userID)) {
            $res = VoteLog::query()->where('userID', $userID)->get();

            foreach ($res as $re) {
                $videoIDs[] = $re->videoID;
            }
        }
        foreach ($videos as $video) {
            $data[] = [
                'videoID'  => $video->id,
                'userName' => $video->userName,
                'videoUrl' => $video->videoUrl,
                'imgUrl'   => $video->imgUrl,
                'votes'    => $video->votes,
                'voted'    => in_array($video->id, $videoIDs),
            ];
        }

        return $this->responseJson(200 ,'success' ,$data);
    }
}
